/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditAdminPassword.java
 *
 * Created on May 3, 2012, 11:35:50 PM
 */
package GUI;

import connection.ManLabConnection;
import entities.Admin;
import utilities.ValidateInput;

/**
 *
 * @author VietLH
 */
public class EditAdminPassword extends javax.swing.JDialog {

    private Admin admin;
    private ValidateInput vi;
    private OptionMessage optionMessage;

    /** Creates new form EditAdminPassword */
    public EditAdminPassword(java.awt.Frame parent, boolean modal, Admin admin) {
        super(parent, modal);
        initComponents();
        this.admin = admin;
        this.vi = new ValidateInput();
        this.optionMessage = new OptionMessage(this);
        hideAllError();
    }

    private void hideAllError() {
        this.lblPasswordNotMatch.setVisible(false);
        this.lblPasswordNull.setVisible(false);
    }

    private boolean checkValidForm() {
        boolean pass = true;

        if (vi.checkIsEmpty(txtPassword.getPassword())) {
            lblPasswordNull.setText("Not allow null");
            lblPasswordNull.setVisible(true);
            pass = false;
        } else if (vi.checkIsEmpty(txtConfirmPassword.getPassword())) {
            lblPasswordNotMatch.setText("Not allow empty");
            lblPasswordNotMatch.setVisible(true);
            pass = false;
        } else if (vi.isExistSpecialCharacter(txtPassword.getPassword())) {
            lblPasswordNull.setText("Not allow special characters");
            lblPasswordNull.setVisible(true);
            pass = false;
        } else if (vi.isExistSpecialCharacter(txtConfirmPassword.getPassword())) {
            lblPasswordNotMatch.setText("Not allow special characters");
            lblPasswordNotMatch.setVisible(true);
            pass = false;
        } else {
            if (!vi.checkMatchPassword(txtPassword.getPassword(), txtConfirmPassword.getPassword())) {
                lblPasswordNull.setText("Password not match");
                lblPasswordNull.setVisible(true);
                pass = false;
            }
        }
        return pass;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblPassword = new javax.swing.JLabel();
        txtConfirmPassword = new javax.swing.JPasswordField();
        lblConfirmPassword = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        lblPasswordNull = new javax.swing.JLabel();
        lblPasswordNotMatch = new javax.swing.JLabel();
        btnClose = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblPassword.setText("Password:");

        lblConfirmPassword.setText("Confirm Password:");

        lblPasswordNull.setForeground(new java.awt.Color(255, 0, 0));
        lblPasswordNull.setText("Not allow null");

        lblPasswordNotMatch.setForeground(new java.awt.Color(255, 0, 0));
        lblPasswordNotMatch.setText("Password not match");

        btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Icons/Cancel-16.png"))); // NOI18N
        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Icons/check2.png"))); // NOI18N
        btnAdd.setText("Update");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setText("Edit Password");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPassword)
                    .addComponent(lblConfirmPassword))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtPassword, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtConfirmPassword, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnClose)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPasswordNull)
                    .addComponent(lblPasswordNotMatch))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(261, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPasswordNull)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPassword))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtConfirmPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblConfirmPassword))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnAdd)
                            .addComponent(btnClose)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblPasswordNotMatch)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(28, 28, 28))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        optionMessage.confirmExit();
    }
        /*}//GEN-LAST:event_btnCloseActionPerformed
    
        private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
    
        /*}//GEN-LAST:event_btnAddActionPerformed
     */

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
        hideAllError();
        if (checkValidForm()) {

            char[] password = txtPassword.getPassword();
            String cPassword = "";
            for (int i = 0; i < password.length; i++) {
                cPassword += password[i];
            }
            admin.setaPassword(cPassword);
            ManLabConnection cnn = new ManLabConnection();
            cnn.createConnection();
            String qry = "update Admin set aPassword='" + cPassword + "'";
            boolean pass = cnn.executeQuery(qry);
            cnn.closeConnection();
            if (pass) {
                optionMessage.showMessage("Password successfully changed!");
                this.dispose();
            }else{
                optionMessage.showErrorMessage("An error occured! Try again later!");
                this.dispose();
            }
        }
    }
    /*}
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(EditAdminPassword.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(EditAdminPassword.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(EditAdminPassword.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(EditAdminPassword.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the dialog */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                EditAdminPassword dialog = new EditAdminPassword(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnClose;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblConfirmPassword;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblPasswordNotMatch;
    private javax.swing.JLabel lblPasswordNull;
    private javax.swing.JPasswordField txtConfirmPassword;
    private javax.swing.JPasswordField txtPassword;
    // End of variables declaration//GEN-END:variables
}
